﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Data.OleDb;
using DavidDB.ADO.Tools;

namespace DavidDB.Reflection.DAL.Managers
{
    public class AccessManager
    {
        /// <summary>
        /// 查询返回表里的所有记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<dynamic> GetList(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            List<dynamic> tList = new List<dynamic>();
            Type type = obj.GetType();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            PropertyInfo[] propertiesa = type.GetProperties();
            string sql = string.Format("select * from [{0}] order by {1} desc", tableName, propertiesa[0].Name);
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans);
            while (reader.Read())
            {
                var t = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
                tList.Add(t);
            }
            reader.Close();
            return tList;
        }

        /// <summary>
        /// 分页返回表里的所有记录
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="PageSize">每页信息量</param>
        /// <param name="NowPage">当前页</param>
        /// <returns></returns>
        public List<dynamic> GetListByPage(OleDbConnection conneciton, OleDbTransaction trans, int PageSize, int NowPage, out int PageCount, out int recordCount, dynamic obj)
        {
            List<dynamic> tList = new List<dynamic>();
            Type type = obj.GetType();
            string nameSpace = type.Namespace;
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            PropertyInfo[] propertiesa = type.GetProperties();
            string sql = "select * from (select top " + PageSize + " * from (select top " + NowPage * PageSize + " * from " + tableName + " order by id desc) order by id) order by id desc";
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans);
            while (reader.Read())
            {
                var t = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
                tList.Add(t);
            }
            recordCount = DBHelper.GetTableCount(tableName, ADO.DbType.Access, propertiesa[0].Name, conneciton, trans);
            if (recordCount % PageSize == 0)
            {
                PageCount = recordCount / PageSize;
            }
            else
            {
                PageCount = (recordCount / PageSize) + 1;
            }
            reader.Close();
            return tList;
        }

        public List<dynamic> GetListByPageLikeKey(OleDbConnection conneciton, OleDbTransaction trans, int PageSize, int NowPage, dynamic obj, out int PageCount, out int recordCount)
        {
            List<dynamic> tList = new List<dynamic>();
            Type type = obj.GetType();
            string nameSpace = type.Namespace;
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            PropertyInfo[] myPropertyInfo2 = type.GetProperties();
            string sqlparam = "";
            int paramCount = 0;
            foreach (PropertyInfo p in myPropertyInfo2)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo2[0], p))
                {
                    sqlparam += "[" + p.Name + "] like @" + p.Name + " and ";
                    paramCount++;
                }
            }
            sqlparam = sqlparam.Substring(0, sqlparam.Length - 4);
            string sql = string.Format("select * from (select top " + PageSize + " * from (select top " + NowPage * PageSize + " * from " + tableName + " where {0} order by id desc) order by id) order by id desc", sqlparam);
            OleDbParameter[] param = new OleDbParameter[paramCount];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo2)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo2[0], p))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }

            }
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans, param);
            while (reader.Read())
            {
                var t = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
                tList.Add(t);
            }
            recordCount = DBHelper.GetTableCountByKeys(tableName, sqlparam, ADO.DbType.Access, myPropertyInfo2[0].Name, conneciton, trans, param);
            if (recordCount % PageSize == 0)
            {
                PageCount = recordCount / PageSize;
            }
            else
            {
                PageCount = (recordCount / PageSize) + 1;
            }
            reader.Close();
            return tList;
        }

        /// <summary>
        /// 按条件分页返回表里的所有记录
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="PageSize">每页信息量</param>
        /// <param name="NowPage">当前页</param>
        /// <returns></returns>
        public List<dynamic> GetListByPageAndKeys(OleDbConnection conneciton, OleDbTransaction trans, int PageSize, int NowPage, dynamic obj, out int PageCount, out int recordCount)
        {
            List<dynamic> tList = new List<dynamic>();
            Type type = obj.GetType();
            string nameSpace = type.Namespace;
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            PropertyInfo[] myPropertyInfo2 = type.GetProperties();
            string sqlparam = "";
            int paramCount = 0;
            foreach (PropertyInfo p in myPropertyInfo2)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo2[0], p))
                {
                    sqlparam += "[" + p.Name + "]=@" + p.Name + " and ";
                    paramCount++;
                }

            }
            sqlparam = sqlparam.Substring(0, sqlparam.Length - 4);
            string sql = string.Format("select * from (select top " + PageSize + " * from (select top " + NowPage * PageSize + " * from " + tableName + " where {0} order by id desc) order by id) order by id desc", sqlparam);
            OleDbParameter[] param = new OleDbParameter[paramCount];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo2)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo2[0], p))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }

            }
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans, param);
            while (reader.Read())
            {
                var t = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
                tList.Add(t);
            }
            recordCount = DBHelper.GetTableCountByKeys(tableName, sqlparam, ADO.DbType.Access, myPropertyInfo2[0].Name, conneciton, trans, param);
            if (recordCount % PageSize == 0)
            {
                PageCount = recordCount / PageSize;
            }
            else
            {
                PageCount = (recordCount / PageSize) + 1;
            }
            reader.Close();
            return tList;
        }



        /// <summary>
        /// 查询按某条件查询的所有记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<dynamic> GetAllByKey(string key, string value, dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            List<dynamic> tList = new List<dynamic>();
            Type type = obj.GetType();
            string nameSpace = type.Namespace;
            PropertyInfo[] propertie = type.GetProperties();
            string tableName = obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            string sql = string.Format("select  * from [{0}] where [{1}]=@{1} order by " + propertie[0].Name + " desc", tableName, key);
            OleDbParameter[] param = new OleDbParameter[1];
            int pIndex = 0;
            foreach (PropertyInfo p in propertie)
            {
                if (p.Name.Equals(key))
                {
                    break;
                }
                pIndex++;
            }
            param[0] = DBHelper.GetParameter(propertie[pIndex]);
            param[0].Value = value;
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans, param);
            while (reader.Read())
            {
                var t = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
                tList.Add(t);
            }
            reader.Close();
            reader.Dispose();
            return tList;
        }

        /// <summary>
        /// 查询一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public dynamic GetModel(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            var t = Activator.CreateInstance(obj.GetType());
            string nameSpace = obj.GetType().Namespace;
            Type type = obj.GetType();
            PropertyInfo[] propertie = type.GetProperties();
            string fullname = obj.ToString();
            string tableName = fullname.Substring(fullname.LastIndexOf(".") + 1);
            string sql = string.Format("select * from {0} where [" + propertie[0].Name + "]=@{1}", tableName, propertie[0].Name);
            OleDbParameter[] param = new OleDbParameter[1];
            param[0] = DBHelper.GetParameter(propertie[0]);
            param[0].Value = propertie[0].GetValue(obj, null);
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans, param);
            while (reader.Read())
            {
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
            }
            reader.Close();
            return t;
        }


        /// <summary>
        /// 保存(sql)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public int Insert(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            int id = -1;
            Type type = obj.GetType();
            string nameSpace = type.Namespace;
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("insert into {0} (", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.PropertyInfoCouldInsert(p, myPropertyInfo[0]))
                {
                    sql += "[" + p.Name + "],";
                }
            }
            sql = sql.Substring(0, sql.Length - 1);
            sql += ") values(";
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.PropertyInfoCouldInsert(p, myPropertyInfo[0]))
                {
                    sql += "@" + p.Name + ",";
                }

            }
            sql = sql.Substring(0, sql.Length - 1);
            sql += ")";
            OleDbParameter[] param = new OleDbParameter[DBHelper.GetSystemNamespaceFieldCount(myPropertyInfo)];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.PropertyInfoCouldInsert(p, myPropertyInfo[0]))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }
            }
            AccessHelper.ExecuteNonQuery(sql, conneciton, trans, param);
            sql = "select max(" + myPropertyInfo[0].Name + ") from " + obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1);
            id = Convert.ToInt32(AccessHelper.ExecuteScalar(conneciton, trans, sql));
            return id;
        }
        /// <summary>
        /// 查询按某些条件查询的头几条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<dynamic> GetTopList(int count, dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            List<dynamic> tList = new List<dynamic>();
            Type type = obj.GetType();
            string nameSpace = type.Namespace;
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select  top {0} * from [{1}] where ", count, obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            int paramCount = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    sql += "[" + p.Name + "]=@" + p.Name + " and ";
                    paramCount++;
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            sql += " order by " + myPropertyInfo[0].Name + " desc";
            OleDbParameter[] param = new OleDbParameter[paramCount];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }
            }
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans, param);
            while (reader.Read())
            {
                var t = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
                tList.Add(t);
            }
            reader.Close();
            return tList;
        }
        /// <summary>
        /// 查询满足条件的记录是否唯一存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool IsExist(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select count(" + myPropertyInfo[0].Name + ") from [{0}] where ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            int paramCount = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    sql += "[" + p.Name + "]=@" + p.Name + " and ";
                    paramCount++;
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            OleDbParameter[] param = new OleDbParameter[paramCount];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }
            }
            int id = Convert.ToInt32(AccessHelper.ExecuteScalar(conneciton, trans, sql, param));
            if (id == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 查询满足条件的记录是否存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool IsJustExist(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select count(" + myPropertyInfo[0].Name + ") from [{0}] where ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            int paramCount = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    sql += "[" + p.Name + "]=@" + p.Name + " and ";
                    paramCount++;
                }
            }
            sql = sql.Substring(0, sql.Length - 4);

            OleDbParameter[] param = new OleDbParameter[paramCount];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }
            }
            int id = Convert.ToInt32(AccessHelper.ExecuteScalar(conneciton, trans, sql, param));
            if (id > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 查询所有满足某些条件的数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public List<dynamic> GetSomeObject(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            List<dynamic> tList = new List<dynamic>();
            Type type = obj.GetType();
            string nameSpace = type.Namespace;
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("select * from [{0}] where ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            int paramCount = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    sql += "[" + p.Name + "]=@" + p.Name + " and ";
                    paramCount++;
                }
            }
            sql = sql.Substring(0, sql.Length - 4);
            sql += " order by " + myPropertyInfo[0].Name + " desc";
            OleDbParameter[] param = new OleDbParameter[paramCount];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }

            }
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, conneciton, trans, param);
            while (reader.Read())
            {
                var t = Activator.CreateInstance(obj.GetType());
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    if (DBHelper.NeedToParse(p))
                    {
                        string columnName = p.Name;
                        string PType = p.PropertyType.ToString();
                        DBHelper.Operator(p, PType, reader[columnName], t);
                    }
                }
                tList.Add(t);
            }
            reader.Close();
            return tList;
        }

        /// <summary>
        /// 执行SQL语句，返回影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connection"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public int ExecuteScalar(string sql, OleDbConnection connection, OleDbTransaction trans)
        {
            int count = AccessHelper.ExecuteNonQuery(sql, connection, trans);
            return count;
        }

        /// <summary>
        /// 执行SQL语句，返回SqlDataReader，需要手动关闭
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connection"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public dynamic ExecuteReader(string sql, OleDbConnection connection, OleDbTransaction trans)
        {
            OleDbDataReader reader = AccessHelper.ExecuteReader(sql, connection, trans);
            return reader;
        }
        /// <summary>
        /// 执行SQL语句，返回查询字段
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connection"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public dynamic GetSingle(string sql, OleDbConnection connection, OleDbTransaction trans)
        {
            var query = AccessHelper.ExecuteScalar(connection, trans, sql);
            return query;
        }


        /// <summary>
        /// 编辑一个对象并保存
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="nameSpace"></param>
        public void Edit(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("update [{0}] set ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            int allCount = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.PropertyInfoCouldEdit(obj, myPropertyInfo[0], p))
                {
                    sql += "[" + p.Name + "]=@" + p.Name + ",";
                    allCount++;
                }
            }
            sql = sql.Substring(0, sql.Length - 1);
            sql += " where [" + myPropertyInfo[0].Name + "]=" + myPropertyInfo[0].GetValue(obj, null);
            OleDbParameter[] param = new OleDbParameter[allCount];
            int cNum = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.PropertyInfoCouldEdit(obj, myPropertyInfo[0], p))
                {
                    param[cNum] = DBHelper.GetParameter(p);
                    param[cNum].Value = p.GetValue(obj, null);
                    cNum++;
                }
            }
            AccessHelper.ExecuteNonQuery(sql, conneciton, trans, param);
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="obj">对象</param>
        public void Delete(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("delete from {0} where " + myPropertyInfo[0].Name + "=@{1}", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1), myPropertyInfo[0].Name);
            OleDbParameter[] param = new OleDbParameter[1];
            param[0] = DBHelper.GetParameter(myPropertyInfo[0]);
            param[0].Value = myPropertyInfo[0].GetValue(obj, null);
            AccessHelper.ExecuteNonQuery(sql, conneciton, trans, param);
        }
        /// <summary>
        /// 删除表里的所有内容
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteAll(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string sql = string.Format("delete from {0} ", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1));
            AccessHelper.ExecuteNonQuery(sql, conneciton, trans);
        }

        /// <summary>
        /// 按照数据的某些字段的值删除记录
        /// </summary>
        /// <param name="obj">对象</param>
        public void DeleteColumByKey_Value(dynamic obj, OleDbConnection conneciton, OleDbTransaction trans)
        {
            Type type = obj.GetType();
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            string cols = "";
            int pIndex = 0;
            foreach (PropertyInfo p in myPropertyInfo)
            {
                if (DBHelper.ParamEnabled(obj, myPropertyInfo[0], p))
                {
                    cols = p.Name;
                    break;
                }
                else
                {
                    pIndex++;
                }

            }
            string sql = string.Format("delete from {0} where {1}=@{2}", obj.ToString().Substring(obj.ToString().LastIndexOf(".") + 1), cols, cols);
            OleDbParameter[] param = new OleDbParameter[1];
            param[0] = DBHelper.GetParameter(myPropertyInfo[pIndex]);
            param[0].Value = myPropertyInfo[pIndex].GetValue(obj, null);
            AccessHelper.ExecuteNonQuery(sql, conneciton, trans, param);
        }
    }
}
